﻿using UnityEngine;
using System.Collections;

public class boxScript : carryableObject {
	
	public float pickUpZPos;
	public interactButton IBut;
	// Use this for initialization
	void Start () {
		try
		{
			IBut = GameObject.FindGameObjectWithTag("InteractButton").GetComponent<interactButton>();
		}
		catch
		{
			;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if(isPlayerIn)
		{
			if(Vector2.Distance(new Vector2(player.transform.position.x,player.transform.position.y)
											,new Vector2(transform.position.x, transform.position.y)) > 2.0f)
			{
				isPlayerIn = false;
			}
		}
	}
	
	void LateUpdate()
	{
		if(isPlayerIn)
		{
			if(player.isGrounded)
			{
				if(Input.GetKeyDown(KeyCode.F))
				{
					if(player.latestObject != this && !player.havePickUpThisTurn)
					{
						if(!player.isCarrying && myParent == null)
						{
							//Debug.Log("GET");
							player.havePickUpThisTurn = true;
							player.boxHold(this);
							pickUpZPos = transform.position.z;
							//this.collider.enabled = false;
						}
					}
				}
			}
		}
		
		if(myParent != null)
		{
			Vector3 tmpRot = transform.eulerAngles;
			
			tmpRot.z = 200 * myParent.direction;
			transform.eulerAngles = tmpRot;
			Vector3 tmpPos = myParent.transform.position + myParent.getSpriteInatePos();

			if(myParent.mySprite.index == 6)
			{
				Debug.Log("IN");
				tmpPos = myParent.transform.position + new Vector3(0.35f * myParent.direction,0.07f,1.0f);
			}

			transform.position = tmpPos;
		}
		else if(!this.collider.enabled)
		{
			this.collider.enabled = true;
		}
	}
}
